package com.microservice.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.microservice.commonutils.JwtUtils;
import com.microservice.commonutils.MD5;
import com.microservice.educenter.entity.UcenterMember;
import com.microservice.educenter.entity.vo.RegisterVo;
import com.microservice.educenter.mapper.UcenterMemberMapper;
import com.microservice.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.microservice.servicebase.exceptionhandler.MicroException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author leesin
 * @since 2022-05-12
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {


    @Override
    public String login(UcenterMember member) {
        //获取登陆手机号
        String mobile=member.getMobile();
        String password=member.getPassword();

        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new MicroException(20001,"登陆失败");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember=baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if (mobileMember==null){
            throw new MicroException(20001,"登陆失败");
        }
        //判断密码
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new MicroException(20002,"密码错误");
        }
        //判断用户是否被禁用
        if (mobileMember.getIsDisabled()){
            throw new MicroException(20001,"登陆失败");
        }

        //登陆成功
         String token=  JwtUtils.getJwtToken(mobileMember.getId(),mobileMember.getNickname());
        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        //获取注册数据
        String code=registerVo.getCode();
        String mobile=registerVo.getMobile();
        String nickname=registerVo.getNickname();
        String password=registerVo.getPassword();

        //非空判断
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)||StringUtils.isEmpty(code)||StringUtils.isEmpty(nickname)){
            throw new MicroException(20001,"注册失败");

        }
        //判断验证码
        if (!code.equals("1111")){
            throw new MicroException(20001,"注册失败");
        }

        //判断手机号是否重复，表里存在相同手机号不进行添加
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count=baseMapper.selectCount(wrapper);
        if (count>0 ){
            throw new MicroException(20001,"手机号已存在");

        }
        //数据添加数据库中
        UcenterMember member =new UcenterMember()  ;
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://img0.baidu.com/it/u=325674188,3280397254&fm=253&fmt=auto&app=138&f=JPEG?w=501&h=500");
        baseMapper.insert(member);
    }


}
